//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    logoName:'水族之家',
    userInfo: {},
    indicatorDots: false,
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    branch_imgs:[],
    isShop:true,
    // isloaded:false
  },
  
  onLoad: function() {
    console.log(this)
    var that = this;
    var latitude =null;
    var longitude = null;
    wx.getLocation({
      type: 'wgs84',  
      success: function (res) {
        latitude = res.latitude;
        longitude = res.longitude;
        that.loadCity(longitude, latitude);
        console.log(res.latitude,res.longitude)
      },
      fail: function (result) {
        that.setData({
          address: "获取定位失败"          
        })
      }
    });
    wx.request({
      url: 'https://xcs.ssxiaoli.com/applets.php/RegionalStores/getPicture/',
      success: function (res) {
        console.log(res)
        var imgs = res.data.data;
        that.setData({
          movies:imgs
        })
      }
    })
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  map: function() {
    console.log(this.data.branch_imgs)
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度  
      success: function (res) {
        var latitude = that.data.lat;
        var longitude = that.data.lng;
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          name: that.data.address,
          address: that.data.address,
          scale: 20
        })       
      }
    })
   
  },
  imgYu:function(event){
    var that=this;
    var urls=that.data.pic;
    console.log(urls)
    var src = event.currentTarget.dataset.src;//获取data-src
    console.log(src)

    console.log(src)
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: urls,// 需要预览的图片http链接列表
      success:function(res){
        console.log(res)
      },
      fail:function(res){
        console.log(res)
      }
    });
  },
  call: function(e){
    console.log(e.currentTarget.dataset.tel)
    var that = this;
    wx.makePhoneCall({
      phoneNumber: e.currentTarget.dataset.tel
    })    
  },
  tiaozhuan:function(e){
    var that=this;
    console.log(e.currentTarget.dataset.url)
    var src;
    if (e.currentTarget.dataset.url){
      if (e.currentTarget.dataset.url.indexOf('?g_id=')) {
        src = e.currentTarget.dataset.url.split('?g_id=');
        wx.navigateTo({
          url: '../ternalLinks/link1?id=' + src[0] + '&lng=' + that.data.lng + '&lat=' + that.data.lat + '&src=' + src[1],
          success: function () {

          },       //成功后的回调；  
          fail: function () { },         //失败后的回调；  
          complete: function () { }      //结束后的回调(成功，失败都会执行)  
        })
      }else{
        src = e.currentTarget.dataset.url;
        wx.navigateTo({
          url: '../ternalLinks/link1?id=' + src + '&lng=' + that.data.lng + '&lat=' + that.data.lat + '&src=' + src[1],
          success: function () {

          },       //成功后的回调；  
          fail: function () { },         //失败后的回调；  
          complete: function () { }      //结束后的回调(成功，失败都会执行)  
        })
      }
      
    } else if (e.currentTarget.dataset.initurl){
      src = e.currentTarget.dataset.initurl;
      // console.log(src)
      wx.navigateTo({
        url: '../ternalLinks/link1?id='+src,
        success: function () {

        },       //成功后的回调；  
        fail: function () { },         //失败后的回调；  
        complete: function () { }      //结束后的回调(成功，失败都会执行)  
      })
    }else{

    }
    
  },
  getUserInfo: function(e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  onShareAppMessage: function () {

  },
  loadCity: function (longitude, latitude) {
    var that=this;
    var page = this;
      
    wx.request({
      url: 'https://api.map.baidu.com/geocoder/v2/?location='+latitude+','+longitude+'&output=json&ak=WEc8RlPXzSifaq9RHxE1WW7lRKgbid6Y',
      success:function(res){
        console.log(res)
        wx.request({
          url: "https://xcs.ssxiaoli.com/applets.php/Regionalstores/RegionalStores/",
          data: {
            'latitude': latitude,
            'longitude': longitude,
            'city': res.data.result.addressComponent.city.split('市')[0]
          },
          method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
          header: { "Content-Type": "application/x-www-form-urlencoded" }, // 设置请求的 header
          success: function (res) {
            if(res.data.data){
              var tels = res.data.data;
              var pic1 = tels.branch_imgs.split(',');
              console.log(pic1)
              var pic = [];
              for (var i = 0; i < pic1.length - 1; i++) {
                pic1[i] = 'http://api.ssxiaoli.com/index.php/picture/index?id=' + pic1[i];
                pic.push(pic1[i]);
              }
              console.log(parseFloat(tels.lat), parseFloat(tels.lng))
              that.BdmapEncryptToMapabc(parseFloat(tels.lat), parseFloat(tels.lng))
              console.log(that.BdmapEncryptToMapabc(parseFloat(tels.lat), parseFloat(tels.lng)))
              that.setData({
                address: tels.address_detail,
                tel: tels.telephone,
                mobile: tels.mobile,
                person: tels.person_name,
                branch_imgs: tels.branch_imgs.split(','),
                // lat: that.BdmapEncryptToMapabc(36.6102793691, 101.7839202340).lat,
                // lng: that.BdmapEncryptToMapabc(36.6102793691, 101.7839202340).lng,
                lat: that.BdmapEncryptToMapabc(parseFloat(tels.lat), parseFloat(tels.lng)).lat,
                lng: that.BdmapEncryptToMapabc(parseFloat(tels.lat), parseFloat(tels.lng)).lng,
                pic: pic,
                initurl: tels.taobao_store_url,
                // isShop:true
              })
            }else{
              that.setData({
                isShop: false
              })
            }
            that.setData({
              isloaded:true
            })
            
            
          },
          fail: function (result) {
          }
        })
      }
     
    })
   
   
  } ,
  MapabcEncryptToBdmap:function (gg_lat, gg_lon) {    
        
    var point= new Object();  
    var x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
    var x = new Number(gg_lon);  
    var y = new Number(gg_lat);    
    var z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * x_pi);    
    var theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);    
    var bd_lon = z * Math.cos(theta) + 0.0065;    
    var bd_lat = z * Math.sin(theta) + 0.006;   
    point.lng = bd_lon;  
    point.lat = bd_lat;  
    //alert("-1:"+point.lng+","+point.lat);  
    return point;  
  } ,
  BdmapEncryptToMapabc:function (bd_lat, bd_lon) {  
    var point= new Object();  
    var x_pi = 3.14159265358979324 * 3000.0 / 180.0;  
    var x = new Number(bd_lon - 0.0065);  
    var y = new Number(bd_lat - 0.006);  
    var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);  
    var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);  
    var Mars_lon = z * Math.cos(theta);  
    var Mars_lat = z * Math.sin(theta);  
    point.lng = Mars_lon;  
    point.lat = Mars_lat;  
    return point;  
  }   
})

